package cn.itsource.exam.controller;

import cn.itsource.exam.domain.Exam;
import cn.itsource.exam.query.ExamQuery;
import cn.itsource.exam.service.ExamService;
import cn.itsource.exam.util.AjaxResult;
import cn.itsource.exam.util.PageList;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/exam")
public class ExamController {
    @Autowired
    private ExamService examService;

    @PostMapping("/class/list")
    public List classList(){
        List list = examService.classList();
        return list;
    }
    @PostMapping("/examList")
    public List examList(){
        List list = examService.examList();
        return list;
    }
    @PostMapping("/list")
    public PageList<Exam> selectExamList(@RequestBody ExamQuery map){
        PageList<Exam> list = examService.selectExamList(map);
        return list;
    }
    @PutMapping("/exam")
    public AjaxResult updateExamList(@RequestBody Exam map){
        examService.updateExam(map);
        return AjaxResult.success();
    }
    @DeleteMapping("/exam/{id}")
    public AjaxResult deleteExamList(@PathVariable("id") String id){
        examService.deleteExam(id);
        return AjaxResult.success();
    }
    @PostMapping("/exam")
    public AjaxResult insertExamList(@RequestBody Exam map){
        map.setCreatetime(new Date());
        examService.insertExam(map);
        return AjaxResult.success();
    }

    /**
     * 答辩数据 班级的ids 和   答辩的id
     * @param map
     * @return
     */
    @PutMapping("/distribution")
    public AjaxResult distribution(@RequestBody Map map){
        //答辩编号id
//        String selectID = map.get("selectNo").toString();
//
//        //答辩班级ids
//        JSONArray selectList1 = JSONObject.parseArray(map.get("selectList").toString());
//        if (StringUtils.isNullOrEmpty(selectID)){
//            return AjaxResult.error("请选择答辩日期");
//        }
        //答辩时间
        String examDate = map.get("examDate").toString();
        if (StringUtils.isNullOrEmpty(examDate)){
            return AjaxResult.error("请选择答辩日期");
        }
        //答辩班级ids
        JSONArray selectList1 = JSONObject.parseArray(map.get("selectList").toString());
        if (null==selectList1||selectList1.size()==0){
            return AjaxResult.error("请选择答辩班级");
        }
        //保存数据
        try {
            examService.saveExam(map);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    @PostMapping("/confirm/{id}")
    public AjaxResult confirmExam(@PathVariable("id") Long id){
        return examService.confirmExam(id);
    }


}
